package app.fugema.shop.main.login.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.base.common.model.http.err.ApiException;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewObserver;

import app.fugema.shop.R;
import app.fugema.shop.databinding.SetNewPasswordFragmentBinding;
import app.fugema.shop.main.login.vm.LoginVM;

public class SetNewPasswordFragment extends BaseFragment<SetNewPasswordFragmentBinding, LoginVM>
{

    public static SetNewPasswordFragment newInstance()
    {
        Bundle args = new Bundle();
        SetNewPasswordFragment fragment = new SetNewPasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SetNewPasswordFragmentBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.set_new_password_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        binding.btnSubmit.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                //支付
                if(((ForgetPassWordActivity) mActivity).isFindPayPwd())
                {
                    setNewPayPwd();
                }
                else
                {
                    setNewPassword();
                }
            }
        });

        binding.etPassWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if (s.toString().length() == 0) {
                    binding.btnSubmit.setEnabled(false);
                    binding.btnSubmit.setAlpha(0.6f);
                }else {
                    binding.btnSubmit.setEnabled(true);
                    binding.btnSubmit.setAlpha(1f);
                }
            }
        });

        String s = binding.etPassWord.getText().toString().trim();
        if (s.length() == 0) {
            binding.btnSubmit.setEnabled(false);
            binding.btnSubmit.setAlpha(0.6f);
        }
    }

    /**
     * 设置新密码
     */
    private void setNewPassword()
    {
        String passWord = binding.etPassWord.getText().toString();
        String passWordRepeat = binding.etPassWordRepeat.getText().toString();

        if (passWord.isEmpty())
        {
            UIUtils.showToastSafes("密码不能为空");
            return;
        }

        if (passWord.length() < 6 || passWord.length() > 20)
        {
            UIUtils.showToastSafes("请设置6-20位新的登录密码");
            return;
        }

        if (!passWord.equals(passWordRepeat))
        {
            UIUtils.showToastSafes("两次密码不一样");
            return;
        }

        viewModel.postChangePwd(((ForgetPassWordActivity) mActivity).getCert(), passWord)
                 .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(getActivity())
                 {
                    @Override
                    protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                    {
                        if( data.data.isSuccess() )
                        {
                            UIUtils.showToastSafes(data.data.getMsg());
                            //滑动到设置结果
                            if (getActivity() instanceof ForgetPassWordActivity)
                            {
                                ForgetPassWordActivity forgetPassWordActivity = (ForgetPassWordActivity) getActivity();
                                forgetPassWordActivity.setChangeResultView();
                            }
                        }
                    }

                    @Override
                    protected void onError(ApiException error)
                    {
                        super.onError(error);
                        UIUtils.showToastSafes(error.getMessage());
                    }
                });
    }

    /**
     * 设置新支付密码
     */
    private void setNewPayPwd()
    {
        String passWord = binding.etPassWord.getText().toString();
        String passWordRepeat = binding.etPassWordRepeat.getText().toString();

        if (passWord.isEmpty())
        {
            UIUtils.showToastSafes("密码不能为空");
            return;
        }

        if (passWord.length() < 6 || passWord.length() > 20)
        {
            UIUtils.showToastSafes("请设置6-20位新的登录密码");
            return;
        }

        if (!passWord.equals(passWordRepeat))
        {
            UIUtils.showToastSafes("两次密码不一样");
            return;
        }

        viewModel.postChangePayPwd(((ForgetPassWordActivity) mActivity).getCert(), passWord)
                .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(getActivity())
                {
                    @Override
                    protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                    {
                        if( data.data.isSuccess() )
                        {
                            UIUtils.showToastSafes(data.data.getMsg());
                            //滑动到设置结果
                            if (getActivity() instanceof ForgetPassWordActivity)
                            {
                                ForgetPassWordActivity forgetPassWordActivity = (ForgetPassWordActivity) getActivity();
                                forgetPassWordActivity.finish();
                            }
                        }
                    }

                    @Override
                    protected void onError(ApiException error)
                    {
                        super.onError(error);
                        UIUtils.showToastSafes(error.getMessage());
                    }
                });
    }

}
